<template>
  <div class="wx_content">
    <div class="conditions">
      <el-row>
        <el-col :span="8"><el-input v-model="wd" placeholder="输入标题关键字搜索" @keyup.enter.native="submitSearch"></el-input></el-col>
        <el-col :span="3" style="margin-left:10px"><el-button type="primary" @click="submitSearch">搜索</el-button></el-col>
        <el-button type="primary" class="fr" @click="$router.push({name: 'ContentTypeWx'})">内容性质管理</el-button>
      </el-row>
      <el-row>
        <span class="label">内容性质：</span>
        <el-select v-model="attrId" placeholder="不限" clearable>
          <el-option v-for="item,n in attrs" :value="item.id" :label="item.name" :key="n.toString()"></el-option>
        </el-select>
      </el-row>
    </div>
    <el-row>
      <el-button @click="$router.push({name: 'EditWxContent', params: {id: 'new'}})" icon="plus">新建内容</el-button>
    </el-row>
    <el-table v-loading="tbLoading" element-loading-text="拼命加载中" :data="newsList.list" stripe class="table">
      <el-table-column label="消息类型" width="400">
        <template slot-scope="scope">
          <div>
            多图文({{scope.row.newsList.length}})
          </div>
          <div v-for="item,n in scope.row.newsList" class="newsItem">
            <img :src="baseUrl + '/content/source/id/'+item.sourceId" alt="" class="preview">
            <p class="content">
              [图文{{n+1}}] <a :href="`${baseUrl}/content/wxnews/preview?logicId=${scope.row.id}&index=${n}`" target="_blank">{{item.title}}</a>
            </p>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="realname" label="创建人">
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{dateFormat(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200"  class-name="is-center">
        <template slot-scope='scope'>
          <el-button type='text' @click="handleEdit(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page.sync="page"
        :page-size.sync="size"
        layout="sizes, total, prev, pager, next, jumper"
        :page-sizes="[10, 30, 50, 100]"
        :total="newsList.page.total"
        @current-change="submitSearch()"
        @size-change="handleSizeChange">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  import {mapActions, mapState} from 'vuex'
  import config from '@/config/env'
  export default {
    data () {
      return {
        wd: '',
        attrId: null,
        sourceType: 1,
        authStatus: null,
        page: 1,
        size: 10,
        tbLoading: false
      }
    },
    mounted () {
      this.initPage()
    },
    computed: {
      ...mapState({
        newsList: state => state.contentManage.wxNewsList,
        attrs: state => state.contentManage.contentType
      }),
      baseUrl () {
        return config.baseUrl
      }
    },
    methods: {
      ...mapActions(['getWxNewsList2', 'getContentTypes']),
      async initPage () {
        this.getContentTypes()
        await this.submitSearch()
      },
      // 日期格式化
      dateFormat (date) {
        return this.$date.format(new Date(date), 'YYYY-MM-DD HH:mm:ss')
      },
      // 搜索
      async submitSearch () {
        this.tbLoading = true
        await this.getWxNewsList2({
          wd: this.wd,
          attrId: this.attrId,
          page: this.page,
          size: this.size
        })
        this.tbLoading = false
      },
      // 改变分页大小
      handleSizeChange (size) {
        this.size = size
        this.submitSearch()
      },
      // 编辑
      handleEdit (id) {
        this.$router.push({
          name: 'EditWxContent',
          params: {id: id}
        })
      }
    }
  }
</script>
<style lang="scss">
  .wx_content{
    .el-row{margin-bottom:15px;}
    .el-table td{padding:5px 0;vertical-align:top;}
    .el-tag{margin-left:10px;height:18px;line-height:16px;}
    .newsItem{
      &{margin-top:10px;overflow:hidden;}
      .preview{float:left;width:100px;height:100px;}
      .content{margin-left:110px;}
      .content a{color:inherit;text-decoration:none;}
      .content a:hover{text-decoration:underline;}
    }
  }
</style>
